import axios from "axios";
import { http, fileUploadHttp } from "../http";
import {
  AddOrUpdateEquipmentQueryType,
  GetEquipmentListQueryType,
} from "../types/equipment";

const baseUrl = "/api/dms/devices";

// 获取设备列表
export const getEquipmentListAPI = (params: GetEquipmentListQueryType) => {
  return http.post(`${baseUrl}/getDevicesPage`, params);
};

// 获取指定用户 指定申请状态  或 指定预约状态设备列表
export interface GetUserDevicesPageRequest {
  page: number;
  size: number;
  userCode: string;
  applicationStatus: string; // APPROVING APPROVED REJECTED CANCELED
  reservationStatus: string; // APPROVING APPROVED REJECTED CANCELED RETURNED
}
export const getUserDevicesPageAPI = (params: any) => {
  return http.post(`${baseUrl}/getUserDevicesPage`, params);
};

// 添加或编辑设备
export const addOrUpdateEquipmentAPI = (
  params: AddOrUpdateEquipmentQueryType
) => {
  return http.post(`${baseUrl}/addOrUpdateDevice`, params);
};

// 删除设备
export const deleteEquipmentAPI = (id: string) => {
  return http.delete(`${baseUrl}/deleteDevice/${id}`);
};

// 根据ID获取设备详情
export const getEquipmentByIdAPI = (id: string) => {
  return http.get(`${baseUrl}/getDeviceById/${id}`);
};

// 批量导入设备
export const batchImportEquipmentAPI = (file: File) => {
  const formData = new FormData();
  formData.append("file", file);
  return fileUploadHttp.post(`${baseUrl}/batchImportDevices`, formData);
};

// 下载模板
export const downloadTemplateAPI = () => {
  return http.get(`${baseUrl}/downloadDeviceImportTemplate`);
};

// 获取全部设备类型
export const typesAPI = () => {
  return http.get(`${baseUrl}/types`);
};

// 更新设备文件
export type UpdateDeviceFilesType = {
  deviceId: string;
  photoIds: string[];
  documentIds: string[];
};
export const updateDeviceFilesAPI = (params: UpdateDeviceFilesType) => {
  return http.put(`${baseUrl}/updateDeviceFiles`, params);
};

// 更新设备状态（报废设备等）
export type UpdateDeviceStatusType = {
  deviceId: string;
  status: string;
};
export const updateDeviceStatusAPI = (params: UpdateDeviceStatusType) => {
  return http.put(
    `${baseUrl}/updateDeviceStatus/${params.deviceId}?status=${params.status}`
  );
};

// 一键备份数据库
export const backupDatabaseAPI = () => {
  return http.get(`/apibackup/database`);
};
